package com.ldd.malphite.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ldd.malphite.dto.BookDto;
import com.ldd.malphite.entity.Book;
import com.ldd.malphite.enums.RunoobStatus;
import com.ldd.malphite.mapper.BookDtoMapper;
import com.ldd.malphite.mapper.BookMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {

    @Autowired
    private BookMapper bookMapper;


    @GetMapping("/selectByGroup")
    public Map<String,String> selectByGroup(){
        Map map = bookMapper.selectByGroup();
        return map;
    }

    @GetMapping("/selectByLimit")
    public PageInfo selecByLimit(){
        PageHelper.startPage(2,3);
        List<Book> books = bookMapper.selectAll();
        PageInfo pageInfo = new PageInfo(books);
        return pageInfo;
    }

    /*动态sql之 if 标签*/
    @GetMapping("/selectByIf")
    public List<Book> selecByIf(){
        Book book = new Book();
        book.setRunoobTitle("aaaa");
        book.setRunoobStatus(RunoobStatus.unsale);
        List<Book> books = bookMapper.selectByIf(book);
        return books;
    }

    /*动态sql之 choose when otherwise 标签*/
    @GetMapping("/selectChoose")
    public List<Book> selecChoose(){
        Book book = new Book();
        book.setRunoobTitle("在人间");
        book.setRunoobAuthor("高尔基");
        book.setRunoobStatus(RunoobStatus.unsale);
        List<Book> books = bookMapper.selectChoose(book);
        return books;
    }

    /*动态sql之 choose when otherwise 标签*/
    @GetMapping("/selectWhere")
    public List<Book> selectWhere(){
        Book book = new Book();
        book.setRunoobTitle("在人间");
        book.setRunoobStatus(RunoobStatus.unsale);
        List<Book> books = bookMapper.selectWhere(book);
        return books;
    }

    /*动态sql之 in 标签*/
    @GetMapping("/selectIn")
    public List<Book> selectIn(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(15);
        List<Book> books = bookMapper.selectIn(list);
        return books;
    }

    @GetMapping("/selectAll")
    public List<Book> selectAll(){
        return bookMapper.selectAll();
    }

    @GetMapping("/insertBook")
    public void insertBook(){
        Book book = new Book();
        book.setRunoobTitle("哆啦A梦");
        book.setRunoobAuthor("嘤嘤嘤");
        book.setRunoobStatus(RunoobStatus.sale);
        book.setSubmissionDate(new Date());
        bookMapper.insert(book);
    }

    @GetMapping("/insertMany")
    public void insertMany(){

        Book book1 = new Book();
        book1.setRunoobTitle("MYSQL45讲");
        book1.setRunoobAuthor("丁奇");
        book1.setRunoobStatus(RunoobStatus.sale);
        book1.setSubmissionDate(new Date());


        Book book2 = new Book();
        book2.setRunoobTitle("深入理解java虚拟机");
        book2.setRunoobAuthor("周志明");
        book2.setRunoobStatus(RunoobStatus.sale);
        book2.setSubmissionDate(new Date());

        List<Book> books = new ArrayList<>();
        books.add(book1);
        books.add(book2);
        bookMapper.insertMany(books);
    }

    @GetMapping("/updateByName")
    public void updateByName(){
        bookMapper.updateByName("深入理解jvm虚拟机","深入理解java虚拟机");
        log.debug("更新成功");
    }

    @Autowired
    private BookDtoMapper bookDtoMapper;

    @GetMapping("/association")
    public List<BookDto> association(){
        return bookDtoMapper.selectAll();
    }

}
